<?php
class Loader {
    //private $controller;
    //private $action;
    private $urlvalues;
    
    // store urlvalues on object creation
    public function __construct($getarray, $postarray)
    {
    	if ($getarray['controller'] == "" || $getarray['action'] == "")
    	{
    		$this->urlvalues['controller'] = 'ui';
    		$this->urlvalues['action'] = 'main';
    		$this->urlvalues['param'] = null;
    	}
    	else
    	{
    		$this->urlvalues['controller'] = $getarray['controller'];
    		$this->urlvalues['action'] = $getarray['action'];
    		unset($getarray['controller'], $getarray['action']);
    		$this->urlvalues['param'] = array_merge($getarray, $postarray);
    	}
    }
    
    // establish the requested controller as an object
    public function CreateController()
    {
        $controllerClassName = $this->urlvalues['controller'] . 'controller';
        // does the class exist?
        if (class_exists($controllerClassName))
        {
            $parents = class_parents($controllerClassName);
            // does the class extend the controller class?
            if (in_array("BaseController", $parents))
            {
                // does the class contain the requested method?
                if (method_exists($controllerClassName, $this->urlvalues['action']))
                {
                    return new $controllerClassName($this->urlvalues['controller'],
	                    							$this->urlvalues['action'], 
	                    							$this->urlvalues['param']);
                }
                else
                {
                    // bad method error
                    return new Error("badUrl", $this->urlvalues['param']);
                }
            }
            else
            {
                // bad controller error
                return new Error("badUrl", $this->urlvalues['param']);
            }            
        }
        else
        {
            // bad controller error
            return new Error("badUrl", $this->urlvalues['param']);
        }
    }
    
}